import React, { Component } from 'react'
// 导入计算模块
import memoizeOne from 'memoize-one'

export default class Memoize extends Component {
  state = {
    num: 2,
    price: 3
  }

  constructor(props) {
    super(props);
    this.add = this.add.bind(this)
  }


  // 记忆功能 连续相同的结果会取上一次结果 缓存了
  getTotal = memoizeOne(
    (a, b) => {
      // console.log('aaa')
      return a * b
    }
  )

  add() {
    this.setState(state => {
      return {
        num: state.num + 1
      }
    })
  }

  render() {
    /* console.log(this.getTotal(1, 2));
    console.log(this.getTotal(1, 2));
    console.log(this.getTotal(1, 2));
    console.log(this.getTotal(2, 2)); */

    return (
      <div>
        小计：{this.getTotal(this.state.num, this.state.price)}
        {/* 小计：{this.getTotal(this.state.num, this.state.price)}
        小计：{this.getTotal(this.state.num, this.state.price)} */}
        <hr />
        <button onClick={this.add}>增加商品数量</button>
      </div>
    )
  }
}
